package ru.kest.babymonitor;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * Setting Manager
 */
public class Settings {

    private static String TAG = "IPCamBabyMonitor";

    private static Settings instance;
    private boolean initialized;

//    private static final String AUDIO_URL = "http://192.168.88.111/audiostream.cgi?user=admin&pwd=&streamid=2&filename=";
//    private static final String VIDEO_URL = "http://192.168.88.111/videostream.cgi?user=admin&pwd=&resolution=32&rate=6";

    private boolean audioOn = false;

    private boolean audioOnAlarmOn = false;

    private boolean alarmOn = false;

    private String serverName;
    private String serverPort;
    private String resolution;
    private String rate;

    private int soundMaxValue; // Triger for sound alarm
    private int latency; // Count of packages with high volume.
    private int soundTimeout; // Timeout in milliseconds beetwen high peaks
    private int alarmPlayingTimeout; // How long sound should be on after alarm detection, sec
    private int soundMultiplier; //

    private Settings() {
    }

    public void loadSettings(Context context) {
//        SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        Resources res = context.getResources();

        audioOn = sharedPref.getBoolean(res.getString(R.string.audioOn_key), false);
        alarmOn = sharedPref.getBoolean(res.getString(R.string.alarmOn_key), false);
        serverName = sharedPref.getString(res.getString(R.string.server_name_key), "192.168.88.111");
        serverPort = sharedPref.getString(res.getString(R.string.server_port_key), "80");
        resolution = sharedPref.getString(res.getString(R.string.resolution_key), "32");
        rate = sharedPref.getString(res.getString(R.string.rate_key), "6");

        soundMaxValue = Integer.parseInt(sharedPref.getString(res.getString(R.string.soundMaxValue_key), "100"));
        latency = Integer.parseInt(sharedPref.getString(res.getString(R.string.latency_key), "5"));
        soundTimeout = Integer.parseInt(sharedPref.getString(res.getString(R.string.soundTimeout_key), "1000"));
        alarmPlayingTimeout = Integer.parseInt(sharedPref.getString(res.getString(R.string.alarmPlayingTimeout_key), "10"));
        soundMultiplier = Integer.parseInt(sharedPref.getString(res.getString(R.string.soundMultiplier_key), "20"));

        Log.d(TAG, "Settings reloaded");
        initialized = true;
    }

    public void saveSettings(Activity activity) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(activity);
        Resources res = activity.getResources();
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(res.getString(R.string.audioOn_key), audioOn);
        editor.putBoolean(res.getString(R.string.alarmOn_key), alarmOn);
        editor.commit();
    }

    public static synchronized Settings getInstance() {
        if (instance == null) {
            instance = new Settings();
        }
        return instance;
    }

    public String getAudioUrl() {
        checkIsInitialized();
        //"http://192.168.88.111/audiostream.cgi?user=admin&pwd=&streamid=2&filename=";
        StringBuilder sb = new StringBuilder();
        if (!serverName.toLowerCase().contains("http://")) sb.append("http://");
        sb.append(serverName).append(":").append(serverPort);
        sb.append("/audiostream.cgi?user=admin&pwd=&streamid=2&filename=");
        return sb.toString();
    }

    public String getVideoUrl() {
        checkIsInitialized();
        //"http://192.168.88.111/videostream.cgi?user=admin&pwd=&resolution=32&rate=6";
        StringBuilder sb = new StringBuilder();
        if (!serverName.toLowerCase().contains("http://")) sb.append("http://");
        sb.append(serverName).append(":").append(serverPort);
        sb.append("/videostream.cgi?user=admin&pwd=&resolution=").append(resolution).append("&rate=").append(rate);
        return sb.toString();
    }

    public boolean isAudioOn() {
        checkIsInitialized();
        return audioOn;
    }

    public boolean isAudioOnAlarmOn() {
        return audioOnAlarmOn;
    }

    public void setAudioOnAlarmOn(boolean audioOnAlarmOn) {
        this.audioOnAlarmOn = audioOnAlarmOn;
    }

    public void setAudioOn(boolean audioOn) {
        this.audioOn = audioOn;
    }

    public boolean isAlarmOn() {
        checkIsInitialized();
        return alarmOn;
    }

    public void setAlarmOn(boolean alarmOn) {
        this.alarmOn = alarmOn;
    }

    public boolean isAudioServiceShouldWork() {
        checkIsInitialized();
        return isAudioOn() || isAlarmOn();
    }

    public int getSoundMaxValue() {
        checkIsInitialized();
        return soundMaxValue;
    }

    public int getLatency() {
        checkIsInitialized();
        return latency;
    }

    public int getSoundTimeout() {
        checkIsInitialized();
        return soundTimeout;
    }

    public int getAlarmPlayingTimeout() {
        checkIsInitialized();
        return alarmPlayingTimeout;
    }

    public int getSoundMultiplier() {
        checkIsInitialized();
        return soundMultiplier;
    }

    public void checkIsInitialized() {
        if (!initialized) throw new RuntimeException("Settings are not initialized");
    }
}
